package com.minxi.jsp.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

/**
 * 数据库连接池工具类
 */
public class DruidUtil {
    /**
     * Druid连接方法
     */
    public static Connection DruidConnect() throws Exception {
        //加载配置文件
        Properties prop = new Properties();
        File file = new File("C:\\Users\\23738\\Downloads\\my-warehouse-jsp\\resources\\druid.properties");
        prop.load(new FileInputStream(file));

        //获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

        //获取数据库连接
        Connection conn = dataSource.getConnection();
        return conn;
    }

    /**
     * Druid关闭方法
     */
    public static void DruidClose(ResultSet resultset, PreparedStatement statement, Connection conn) throws Exception {
        if (null != resultset) {
            resultset.close();
        }
        if (null != statement) {
            statement.close();
        }
        if (null != conn) {
            conn.close();
        }
    }
    public static void DruidClose(PreparedStatement statement, Connection conn) throws Exception {
        if (null != statement) {
            statement.close();
        }
        if (null != conn) {
            conn.close();
        }
    }
}